init("0", 0); --以当前应用 Home 键在右边初始化init(0,1)
GlobalX,GlobalY = -1,-1;
function ClickOn(delay,x,y)
	if x > -1 then
		mSleep(delay);
		touchDown(1,x,y);
		mSleep(delay)
		touchUp(1,x,y);
	end
end
Gtable = {
	["Quit"] = {50,45}
	}
Quit = {}
Array = {
	["0-0"]={"小地图界面",{5,756,141,1025},"0|0|0xd44e36,5|3|0xf2563e,11|72|0xb37d3a,23|69|0x986b32,-1|145|0xcdae80,11|144|0xe0cda7,29|65|0xc89e5f,-14|76|0x725128,-19|-24|0x5a3f1f,17|-13|0xfee8d9,14|0|0x844d23",95,0,0,0},
	["0-1"]={"城堡",{573,987,719,1128},"0|0|0x70788f,-26|-13|0x534d49,-38|-9|0xdfce9c,-37|17|0xa99a77,-42|-27|0x735e44,-17|-37|0xca4840,7|-35|0xdccfb1,-12|-23|0x33281d",95,0,0,0},
	["0-2"]={"地下城-下一关按钮",{581,467,715,623},"0|0|0x968884,-1|4|0x968884,-4|13|0x938581,-11|-1|0x573e2d,-5|-16|0xd4c6b5,9|-5|0xe7e3da,10|7|0x3a2e28,-6|11|0x9b8b85,15|18|0x092029,-26|3|0x081e26,19|-22|0x05151e",95,0,0,0},
	["1-0"]={"选择英雄界面-进入",{18, 1089, 688, 1270},"0|0|0xeeddcc,1|8|0xf2e8d7,9|16|0xe2d5c5,12|13|0xf4ecda,33|10|0xf2e9d8,33|17|0xc9b5a7,-43|58|0xcbaf6d",95, 0, 0, 0},
	["1-1"]={"选择英雄界面-向左",{14, 423, 715, 1278},"0|0|0xffdc57,26|-31|0xe4912d,2|7|0xfff05f,13|18|0xfff55b,28|31|0xffff68,601|-32|0xffff64,613|-22|0xfff85c,626|-3|0xffd54a,625|5|0xffc64a,617|11|0xffd470,605|22|0xeb9b30,600|30|0xe3902e,218|556|0x722015,245|559|0x781f14,304|565|0xd8c3b4,321|567|0xab887c,330|572|0x6b3d37,274|618|0xb29b63,277|618|0x57543f,290|617|0x212a29",95, 0, 0, 0},
	["1-2"]={"选择英雄界面-向右",{130, 469, 711, 1264},"0|0|0xffce48-0x111111,-9|14|0xffce68,-19|24|0xee9e31,-23|-31|0xffff64,-12|-14|0xffe353,-308|574|0x8a635a,-321|579|0xb1988c,-331|578|0xdfd0c0,-332|569|0x9f7a6e,-330|614|0xd8b56f,-245|560|0x7f2215,-352|567|0x5f1b14",95, 0, 0, 0},
	["1-3"]={"选择英雄界面-神罚之翼",{10, 79, 716, 1270},"0|0|0xefcb7c-0x222222,-1|9|0xb09b66-0x222222,10|8|0xc7ac6e-0x222222,8|-5|0xb19c67-0x222222,35|-5|0xb39d68-0x222222,50|-5|0xe5c379,47|8|0xe7c479,58|10|0xf5cf7e,77|5|0xeeca7b,74|1|0xd0b472,68|-8|0xd5b773",95, 0, 0, 0},
	["1-4"]={"选择英雄界面-选择按钮",{170,1180,550,1275},"0|0|0xdcc3b3-0x111111,0|10|0xc6afa2-0x111111,10|16|0xb2998d-0x111111,32|3|0xf7eedc-0x111111,37|5|0xf3e9d8-0x111111,33|16|0xc9b5a7-0x111111,50|-11|0xa32310-0x111111,-43|49|0xbda164-0x111111,-48|59|0xbfa568-0x111111,-1|56|0xb69e64-0x111111,37|56|0x2e342e-0x111111,45|63|0xfdd680-0x111111",95,0,0,0},
	["1-5"]={"NPC-对话金矿",{12,898,709,1277},"0|0|0xe2ddbd-0x111111,0|12|0xcfccb1-0x111111,5|10|0x162a3e-0x111111,18|9|0x162e43-0x111111,39|5|0xcbc9af-0x111111,144|27|0xa7aa99-0x111111,113|32|0x58676c-0x111111,83|39|0x77837f-0x111111,60|42|0x162e43-0x111111,9|65|0x425965-0x111111,-186|-32|0x542977-0x111111,-196|13|0xa57342-0x111111,-156|28|0x9781be-0x111111,-160|61|0x643388-0x111111,-153|81|0x9833ba-0x111111",95,0,0,0},
	["1-6-0"]={"指南-金矿",{7,824,715,1274},"0|0|0xcfae68,-30|-16|0xa9946a,-34|-11|0x796d59,13|9|0x515760,8|19|0x9095ad,-12|19|0x644b28,21|31|0xa58551",95,0,0,0},
	["1-6-1"]={"选择-金矿",{2,917,717,1175},"0|0|0xd9bb77,-18|-49|0xc18d37,-7|-49|0xba812b,-9|-49|0xb27216,-10|-43|0xb77c24,-17|-42|0xbf8a35,-4|-41|0xb98028,2|59|0x24230d,-2|49|0xc5c58d,-17|60|0xc3be79,-24|64|0xb6ae5e,-10|38|0x8b8648,17|34|0x605d35,9|37|0x858147",95,0,0,0},
	["1-6-2"]={"选择-建造",{3,793,719,1275},"0|0|0x759f5d,-9|-5|0xf2e53f,-9|3|0x14636a,-10|10|0x3f795c,2|12|0xfdeb3c,6|12|0xfdeb3c,1|4|0xe9e040,3|-1|0xbbc649,-1|-1|0xadbf4e,19|-2|0xabbe4e,12|-5|0xb2c14c,14|7|0xb9c349,11|12|0x739752,-43|18|0x12595e,-42|-12|0x0e6a72,40|-24|0x13757d,73|-7|0x106870",95,0,0,0},
	["2-0-0"]={"选择-人物图标",{4,4,145,135},"0|0|0x201e28,-4|15|0x1d1a27,-1|25|0xa4815f,19|19|0xdec29c,5|16|0x907760,64|-22|0x21e402,71|3|0x20ec00,44|54|0x251e0c",95,0,0,0},
};
ArraySingleFixedPoint = {
	--[[["NPC-1"]={"第一个NPC",432,1160},
	["NPC-Select-0"]={"选择建筑",660,1060},
	["NPC-Select-1"]={"选择建筑",100,1030},
	["NPC-Select-2"]={"选择建筑",300,1030},
	["NPC-Select-3"]={"选择建筑",500,1030},
	["NPC-Select-4"]={"建造",540,1210},
	["NPC-Select-5"]={"建造",340,600},]]
	["1-0"]={"训练士兵",{{350,600}, {470,730} ,{80,900},{500,1210},{200,1200},{50,40}}},
	["2-0"]={"奖励-建筑",{{50,1063},{537,1215}}},
	["2-1"]={"奖励-领取",{{52,890},{184,817}}},
	["3-0"]={"龙蛋",{{360,620},}},
	["4-0"]={"建造-主城",{{537,1215},}},
	["5-0"]={"邮件",{{500,1228},Gtable["Quit"]}},
	["6-0"]={"自动喊话",{{305,1150},{75,1228}}},
	["7-0"]={"人物",{{60,52},}},
	


};

function isFindCoordinate(selectIndex)
	local tmpArray = Array[selectIndex]
	GlobalX,GlobalY = -1,-1;
	GlobalX,GlobalY = findColor(tmpArray[2],tmpArray[3],tmpArray[4],tmpArray[5],tmpArray[6],tmpArray[7])
	if GlobalX ~= -1 then
		return true
	else
		return false	
	end
end
function myToast(delay,content)
	toast(content)
	mSleep(delay)
end
function isSelectHero(hero)
	local count = 0
	while not isFindCoordinate(hero) do
		if isFindCoordinate("1-1") then
			ClickOn(200,GlobalX,GlobalY)
		end
		count = count + 1;
		print(count)
		if count > 50 then
			myToast(1000,"没有进入到" .. string.sub(Array[hero][1],20,-1))
			return false
		end
	end
	myToast(1000,"找到了" .. string.sub(Array[hero][1],20,-1))
	return true
end


--ClickOn(200,ArraySingleFixedPoint["NPC-Select-0"][2],ArraySingleFixedPoint["NPC-Select-0"][3])
--isFindCoordinate("1-6-2")
--ClickOn(200,GlobalX,GlobalY)

function runBuildArchitecture(head,FixedCount)
	--print(ArraySingleFixedPoint[head][2][1][1])
	local count = 0
	while count < FixedCount do
		for k,v in pairs(ArraySingleFixedPoint[head][2]) do
			print(v[1] .. "," .. v[2])
			ClickOn(200,v[1],v[2])
		end
		count = count + 1
	end
end

function ScreenSlide(delay,point_1,point_2)
	touchDown(1,point_1[1],point_1[2])
	mSleep(delay)
	touchMove(1,point_2[1],point_2[2])
	mSleep(delay)
	touchUp(1,point_2[1],point_2[2])  
end

--runBuildArchitecture("6-0") -- {625,1228}
function AutoSendMessage(delay,content,sendTable)
	inputText("#CLEAR#")
	inputText(content)
	ClickOn(delay,sendTable[1],sendTable[2])
	mSleep(delay)
end
--ClickOn(200,50,40)

--156,16,258,44
function MatchWord(point1,point2)
	local ocr, msg = createOCR({
		type = "tesseract", -- 指定tesseract引擎
		path = "[external]", -- 使用开发助手/叉叉助手的扩展字库
		lang = "eng_ext" -- 使用英文增强字库(注意需要提前下载好)
	})
	if ocr ~= nil then
    string.trim = function(s)
        return s:match'^%s*(.*%S)' or ''
    end
    local code, text = -1, ""
    mSleep(1000)
    -- 识别屏幕图像内容(例如5/5s设备的当前时间显示)
    code, text = ocr:getText({
        rect = {641, 4, 712, 40},
        --rect = {1, 1, 720, 960},
        diff = {"0xdbdbdb-0xdddddd"}, -- 时间颜色为纯黑
        whitelist = "0123456789APM:" -- 添加所有可能出现的字符作为白名单
        --whitelist = "巨龙之战" -- 添加所有可能出现的字符作为白名单
    })
    -- 输出示例："code = 0, text = 5:24 PM" (末尾可能有多余换行符，为正常结果，可以参考trim处理)
    sysLog("code = " .. tostring(code) .. ", text = " .. text)
	end
end
--runBuildArchitecture("7-0",2)
function Dixiacheng()
	if isFindCoordinate("0-0") then
		ClickOn(20,48,1225)  --点击圆球进入城堡
		while not isFindCoordinate("0-1") do
			mSleep(1000)
		end
		if isFindCoordinate("0-1") then
			myToast(1000,"找到了想要的")
			ScreenSlide(200,{80,800},{600,800})
			ScreenSlide(200,{80,800},{600,800})
			ClickOn(300,390,540) -- 点击英雄神庙
			ClickOn(300,360,720) -- 点击地下城--位置可能有变
			mSleep(2000)
			ClickOn(300,354,650) -- 点击【确定】离线经验
			while true do
				local count = 0;
				while count < 200 do
					count = count + 1
					ClickOn(20,512,390)  -- 点击打怪
				end
				
				--[[
				mSleep(1000)
				if isFindCoordinate("0-2") then  --进入下一关
					ClickOn(50,GlobalX,GlobalY)
				end]]
			end
		end
	end
end
--Dixiacheng()
function Dixiacheng_zhijieda()
	while true do
		local count = 0;
		while count < 50 do
			count = count + 1
			ClickOn(20,512,390)  -- 点击打怪
		end
	--[[
		mSleep(1000)
		if isFindCoordinate("0-2") then  --进入下一关
			ClickOn(50,GlobalX,GlobalY)
		end
		]]
	end
end
function LianMengZhuanXian()
	ClickOn(200,660,1230)
	mSleep(3000)
	ClickOn(200,270,900)
	mSleep(3000)
	local count = 0
	while count < 20 do
		ClickOn(300,350,1100)
		count = count + 1
	end
end
Dixiacheng_zhijieda()
--LianMengZhuanXian()
function GoGlobal()  -- 进入全局地图界面
	if isFindCoordinate("0-0") then
		return
	else
		ClickOn(200,48,1225) --点击圆球进入全局大地图
		--ClickOn(200,GlobalX,GlobalY)
	end
end
--GoGlobal()